package com.shujia.base;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.io.IOException;

public class Code04CreateTable {
    static Connection connection;

    static {
        // 创建连接需要给定连接信息，所以需要给定配置类
        Configuration conf = new Configuration();

        // 由于对HBASE进行操作时，需要连接其Zookeeper 那么需要传入Zookeeper的IP
        conf.set("hbase.zookeeper.quorum", "node1,node2,master");
        // 获取连接对象 通过ConnectionFactory来创建连接对象

        try {
            connection = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws IOException {
        Admin admin = connection.getAdmin();

        TableName tableName = TableName.valueOf("api:tb1");

        if (!admin.tableExists(tableName)) {
            // 创建表时，需要设置其列族信息
            HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);

            HColumnDescriptor info1 = new HColumnDescriptor("info1");
            hTableDescriptor.addFamily(info1);

            HColumnDescriptor info2 = new HColumnDescriptor("info2");
            hTableDescriptor.addFamily(info2);

            admin.createTable(hTableDescriptor);

        }else {
            System.out.println("表已存在");
        }
        admin.close();
        connection.close();

    }
}
